﻿using System;

namespace TimeCalendar
{
    public class GregorianDate
    {
        public Eras Era { get; private set; }
        public int Year { get; private set; }
        public byte Month { get; private set; }
        public byte Day { get; private set; }

        public Years YearType { get; private set; }
        public Months MonthType { get; private set; }
        
        public GregorianDate(Eras era, int year, byte months, byte days)
        {
            if (era == Eras.AD || era == Eras.BC)
            {
                Era = era;
                if (year <= int.MaxValue && year >= int.MinValue)
                {
                    Year = year;
                    if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
                        YearType = Years.Leap;
                    if (Year % 4 != 0 || (Year % 100 == 0 && Year % 400 != 0))
                        YearType = Years.NotLeap;
                    if (months >= 1 && months <= 12)
                    {
                        Month = months;
                        MonthType = (Months)months;
                        if (days >= 1 && days <= 31)
                        {
                            Day = days;
                            switch (MonthType)
                            {
                                case Months.January:
                                case Months.March:
                                case Months.May:
                                case Months.July:
                                case Months.August:
                                case Months.October:
                                case Months.December:
                                    {
                                        if (Day > (byte) Days.Months31)
                                            throw new Exception("There are no more than 31 day in this month");
                                        break;
                                    }
                                case Months.April:
                                case Months.June:
                                case Months.September:
                                case Months.November:
                                    {
                                        if (Day > (byte) Days.Months30)
                                            throw new Exception("There are no more than 30 day in this month");
                                        break;
                                    }
                                case Months.February:
                                    {
                                        if (YearType == Years.Leap && days > (byte)Days.FebruaryLeap)
                                            throw new Exception(
                                                "There are no more than 29 days in February in leap year ");
                                        if (YearType == Years.NotLeap && days > (byte)Days.FebruaryDefault)
                                            throw new Exception(
                                                "There are no more than 28 days in February in not leap year ");
                                        break;
                                    }
                            }
                        }
                    }
                }
            }
        }


    }
}
